//
//  HTClassCalculationTool.swift
//  QuitSmokingApp
//
//  Created by   on 2024/3/13.
//

import Foundation

class HTClassCalculationTool {
    /// 获取全部计划
    static func ht_getAllPlanData() -> [String: [(String, SmokingCessationPlanModel)]] {
        let var_all_data = HTClassStorage.ht_getAllPlan()
        return ht_processPlanData(var_all_data)
    }
    /// 根据年月返回该月份的数据
    static func ht_getPlanDataKeyValue(_ date: String) -> [(String, SmokingCessationPlanModel)]? {
        let var_data = ht_getAllPlanData()
        var var_key = date
        if date.contains(".") {
            var_key = var_key.replacingOccurrences(of: ".", with: "-")
        }
        return var_data[var_key]
    }
    
    /// 根据年月返回该月份Value的数据
    /// - Parameter date: 日期
    /// - Returns:
    static func ht_getPlanDataValue(_ date: String) -> [SmokingCessationPlanModel] {
        let var_data = ht_getAllPlanData()
        var var_key = date
        if date.contains(".") {
            var_key = var_key.replacingOccurrences(of: ".", with: "-")
        }
        if let var_data_array = var_data[var_key] {
            var var_values: [SmokingCessationPlanModel] = []
            for (_, value) in var_data_array {
                var_values.append(value)
            }
            return var_values
        }
        return []
    }
    /// 返回排序后的数据
    static func ht_processPlanData(_ var_data: [String: SmokingCessationPlanModel]) -> [String: [(String, SmokingCessationPlanModel)]] {
        // 按照年月将数据进行区分整理
        var var_sortedData: [String: [(String, SmokingCessationPlanModel)]] = [:]

        for (var_key, var_value) in var_data {
            let var_yearMonth = String(var_key.prefix(7))
            if var_sortedData[var_yearMonth] == nil {
                var_sortedData[var_yearMonth] = []
            }
            var_sortedData[var_yearMonth]?.append((var_key, var_value))
        }

        // 对每个年月的数据进行顺序排序
        for (var_key, _) in var_sortedData {
            var_sortedData[var_key]?.sort(by: { $0.0 < $1.0 })
        }

        return var_sortedData
    }
    
    /// 过滤出指定月份的签到数据
    static func ht_filterSignedDatesInSameMonth(var_dates: [String], var_referenceDate: Date) -> [String] {
        let var_calendar = Calendar.current
        let var_referenceComponents = var_calendar.dateComponents([.year, .month], from: var_referenceDate)
        
        let var_filteredDates = var_dates.filter { var_date in
            let var_formatter = DateFormatter()
            var_formatter.dateFormat = "yyyy-MM-dd"
            let var_dateComponents = var_calendar.dateComponents([.year, .month], from: var_formatter.date(from: var_date)!)
            return var_dateComponents.year == var_referenceComponents.year && var_dateComponents.month == var_referenceComponents.month
        }
        
        return var_filteredDates
    }
    
    /// 判断给定日期是否是当前月
    static func ht_isCurrentMonth(var_referenceDate: Date) -> Bool {
        let var_calendar = Calendar.current
        let var_referenceComponents = var_calendar.dateComponents([.year, .month], from: var_referenceDate)
        let var_dateComponents = var_calendar.dateComponents([.year, .month], from: Date())
        return var_dateComponents.year == var_referenceComponents.year && var_dateComponents.month == var_referenceComponents.month
    }

    // 获取最近7天的数据
    static func ht_getRecentData() -> [SmokingCessationPlanModel] {
        let var_data = ht_getAllPlanData()
        var var_dataArray: [SmokingCessationPlanModel] = []
        var var_keys = Array(var_data.keys)
        // 排序key
        var_keys.sort { (date1, date2) -> Bool in
            return date1 > date2
        }
        for var_key in var_keys {
            let var_item_array = var_data[var_key]!
            for var_data_item in var_item_array.reversed() {
                var_dataArray.append(var_data_item.1)
                if var_dataArray.count == 7 {
                    return var_dataArray.reversed()
                }
            }
        }
        return var_dataArray
    }
}
